package top.chmin.project.cloud.builder;

import org.springframework.stereotype.Component;

import java.util.stream.Collectors;

/**
 * chmin
 * 2023/12/15 15:21
 * cloud-builder
 */
@Component
public class SaveBuilder implements Builder {
    @Override
    public void build(BuilderConfig config) {
        String content = """
                package top.chmin.project.cloud.#e.module#.#e.pack#.auto;
                
                #import#
                                
                /**
                 * chmin
                 * #dateTime#
                 * cloud-builder
                 */
                public interface #e.simpleName#Save {
                                
                    @PostMapping("/au/#e.fieldName#/save")
                    int save(@RequestBody Param param);
                
                    @Getter
                    @Setter
                    @Accessors(chain = true)
                    class Param {
                %s
                    }
                
                }
                """.formatted(
                config.getEntityColumnList().stream()
                        .filter(f -> !"id".equals(f.getFieldName()))
                        .filter(f -> !"createBy".equals(f.getFieldName()))
                        .filter(f -> !"createTime".equals(f.getFieldName()))
                        .filter(f -> !"updateBy".equals(f.getFieldName()))
                        .filter(f -> !"updateTime".equals(f.getFieldName()))
                        .map(f -> fixEntityColumn(f, "        // #f.remark#\n        #f.simpleType# #f.fieldName#;"))
                        .collect(Collectors.joining("\n"))
        );

        save(config, config.getEntity().getClassName() + "Save", content);
    }
}
